import { request } from '@/plugins/request'

/* 获取文章列表
 * 可选参数：tag/author/favorited/limit/offset
 * */
export const listArticle = params => {
  return request({
    url: '/api/articles',
    method: 'GET',
    params,
  })
}

/* 获取feed文章列表
 * 可选参数：limit/offset
 * */
export const feedArticle = params => {
  return request({
    url: '/api/articles/feed',
    method: 'GET',
    params,
  })
}

/* 获取指定文章 */
export const getArticle = slug => {
  return request({
    url: `/api/articles/${slug}`,
    method: 'GET',
  })
}

/* 删除指定文章 */
export const deleteArticle = slug => {
  return request({
    url: `/api/articles/${slug}`,
    method: 'DELETE',
  })
}

/* 新增文章 */
export const createArticle = data => {
  return request({
    url: '/api/articles',
    method: 'POST',
    data: {
      article: data,
    },
  })
}

/* 更新文章 */
export const updateArticle = (data, slug) => {
  return request({
    url: `/api/articles/${slug}`,
    method: 'PUT',
    data: {
      article: data,
    },
  })
}

/* 添加评论 */
export const addComments = (slug, data) => {
  return request({
    url: `/api/articles/${slug}/comments`,
    method: 'POST',
    data: {
      comment: { body: data },
    },
  })
}

/* 获取评论列表 */
export const getComments = slug => {
  return request({
    url: `/api/articles/${slug}/comments`,
    method: 'GET',
  })
}

/* 删除指定评论 */
export const deleteComments = (slug, id) => {
  return request({
    url: `/api/articles/${slug}/comments/${id}`,
    method: 'DELETE',
  })
}

/* 点赞指定文章 */
export const favoriteArticle = slug => {
  return request({
    url: `/api/articles/${slug}/favorite`,
    method: 'POST',
  })
}

/* 取消点赞 */
export const unfavoriteArticle = slug => {
  return request({
    url: `/api/articles/${slug}/favorite`,
    method: 'DELETE',
  })
}
